<?php

use com\gdl\kq\interfaces\AttAdminBulletinServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\kq\dataobject\bulletin\bulletinDTO;


class api_AttAdminBulletinService extends api_Abstract implements AttAdminBulletinServiceIf
{

	//添加公告
    public function addBulletin(bulletinDTO $bulletinDTO)
    {
		$result = new ResultDO();
		$datas = $bulletinDTO->filename ? $bulletinDTO->filename : [];
			
		try{
			$result->code = 0;
			$result->success = true;
			/*****/

            if(empty($datas['content'])){
				 $result->message = "请填写公告内容";
				 return $result;
			}
			$bulletin = new model_attendance_bulletin();
			$appadminuserModel = new model_lhbase_appadminuser();
            $appadminuserInfo = $appadminuserModel->selectOne(
                " lh_app_admin_user.id ={$datas['adminUserId']} ",
                'lh_app_admin_user.id,lh_app_admin_user.realName publisher,lp.platformName',
                "",
                "",
                array("lh_platform as lp"=>"lp.id=lh_app_admin_user.platformId"));
            if (!$appadminuserInfo){
                $result->message = "添加失败";
                return $result;
            }
				$data['isdelete'] = 1;
				$data['status'] = $datas['status'];
				$data['publisher'] = $appadminuserInfo['publisher'];
				$data['adminUserId'] = $datas['adminUserId'];
				$data['content'] = $datas['content'];
				$data['platformId'] = $datas['platformId'];
				$data['adminReg'] = $datas['adminReg'];
				$data['platformName'] = $appadminuserInfo['platformName'];
				$data['releaseTime'] = date('Y-m-d H:i:s');
				$data['updateTime'] = date('Y-m-d H:i:s');
				$bulletin->insert($data);
			/******/
				$result->message = '成功';
				//$result->message = json_encode($platformDe);
                $result->code = 1;


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }

	//查看公告
	 public function lookOver(bulletinDTO $bulletinDTO)
    {
			$result = new ResultDO();
			$id = $bulletinDTO->id ? gdl_lib_BaseUtils::getStr($bulletinDTO->id,'int') : 0;

		try{
			/**********/
			$result->code = 0;
            $result->success = true;
            if(!$id){
				$result->message = "缺少参数ID";
				return $result;
			}
			$paper = new model_attendance_bulletin();
			$paperArr = $paper->selectOne(['id'=>$id],'content,status');
			if(empty($paperArr)){
				$result->message = "记录不存在";
			} else {
				$result->code = 1;
				$data['content'] = $paperArr['content'];
				$data['status'] = $paperArr['status'];

				$result->data[] = $data;
			}
            /*********/


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	//修改公告
	 public function editBulletin(bulletinDTO $bulletinDTO)
    {
		$result = new ResultDO();
		$datas = $bulletinDTO->filename ? $bulletinDTO->filename : [];
		$id = $bulletinDTO->id ? gdl_lib_BaseUtils::getStr($bulletinDTO->id,'int') : 0;
		try{
			$result->code = 0;
			$result->success = true;
			/*****/

            if(empty($datas['content'])){
				 $result->message = "请填写公告内容";
				 return $result;
			}
			$bulletin = new model_attendance_bulletin();
				$data['content'] = $datas['content'];
				$data['status'] = $datas['status'];
				$data['updateTime'] = date('Y-m-d H:i:s');
				$bulletin->update('id='.$id,$data);
			/******/
				$result->message = '成功';
				//$result->message = json_encode($bulletin);
                $result->code = 1;


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}
	//批量删除公告
	 public function deleteBulletin(bulletinDTO $bulletinDTO)
    {
		$result = new ResultDO();
        $filename = $bulletinDTO->filename ? $bulletinDTO->filename : [];
        $bulletinid = $bulletinDTO->ids ? explode(',',$bulletinDTO->ids) : [];
		try{
			$result->code = 0;
			$result->success = true;
			/*****/

            if(empty($bulletinid)){
				 $result->message = "请选中删除内容";
				 return $result;
			}
			$bulletin = new model_attendance_bulletin();
            $id = [0];
            foreach($bulletinid as $v){
                $id[] = (int)$v;
            }
            $condition = "id in('".implode("','",$id)."')";//'admin_reg'=>$reg,


            if(!empty($filename)){
                if(!empty($filename['isdelete']) && $filename['isdelete'] == 'removes'){
                    $bulletin->delete($condition);
                } else {
                    $bulletin->update($condition,$filename);
                }
            }
			/******/
				$result->message = '成功';
				//$result->message = json_encode($bulletin);
                $result->code = 1;


            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}

	//公告列表
	 public function getBulletinList(bulletinDTO $bulletinDTO)
    {
		$result = new ResultDO();
		$filename = $bulletinDTO->filename ? $bulletinDTO->filename : [];
        $id = $bulletinDTO->id ? $bulletinDTO->id : 0;

		$platform = $bulletinDTO->filename['platform'] ?  gdl_lib_BaseUtils::getStr($bulletinDTO->filename['platform'], 'string') : '';
		$platformId = $bulletinDTO->filename['platformId'] ?  $bulletinDTO->filename['platformId'] : 0;
		$title = $bulletinDTO->filename['title'] ?  gdl_lib_BaseUtils::getStr($bulletinDTO->filename['title'], 'string') : '';
		$status = $bulletinDTO->filename['status'] ?  gdl_lib_BaseUtils::getStr($bulletinDTO->filename['status'], 'string') : '';
		$paper = [];

		$adminReg = $bulletinDTO->filename['adminReg'] ?  gdl_lib_BaseUtils::getStr($bulletinDTO->filename['adminReg'], 'string') : '';
			//现在还没有启动平台id 所以下面只接差所有

			$bulletin = new model_attendance_bulletin();

			//$condition  = "adminReg='{$adminReg}'";


            try {
                if ($id){
                    $num = 1;
                    $condition  = " id = {$id} ";
                    $items = 'id,content,updateTime,status,platformName';
                    $groupBy = ' order by id desc ';
                    $res = $bulletin->selectOne($condition, $items, $groupBy);
                    $res = $res?[$res]:[];
                }else{
                    $condition  = " adminReg!='' and isdelete=1 and platformId = $platformId";
                    if($title!=''){
                        $condition .= " and content like'%".$title."%'";
                    }
                    if($platform){
                        $condition .= " and platformName like'%".$platform."%'";
                    }

                    //状态
                    if($status=='starts'){//开启
                        $condition .= ' and status=1';
                    } else if($status=='disabled'){//禁用
                        $condition .= ' and status=0';
                    }

                    $page = ($filename['page']-1) * $filename['pagesize'];
                    $items = 'id,content,updateTime,status,platformName';
                    $groupBy = ' order by id desc  limit '.$page.','.$filename['pagesize'];
                    $num = $bulletin->selectOne($condition, 'count(*) num');
                    $res = $bulletin->select($condition, $items, $groupBy)->items;
                }


                //进行平台顶级读取处理

                if (!empty($res)) {

                    $result->data = $res;
                    $result->counts =  $num['num'];
//					$result->message = $num['num'];
                    $result->code = 1;

                } else {
                    $result->code = 0;
					 $result->message = 0;
                }
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }

            return $result;

	}

	//批量开启和关闭
	public function stopBulletin(bulletinDTO $bulletinDTO){
			 $result = new ResultDO();
        try {

			$filename = $bulletinDTO->filename ? $bulletinDTO->filename : [];
			$bulletinId = $bulletinDTO->ids ? explode(',',$bulletinDTO->ids) : [];

			$bulletinModel = new model_attendance_bulletin();
			$id = [0];
			foreach($bulletinId as $v){
				$id[] = (int)$v;
			}
			$condition = "id in('".implode("','",$id)."')";//'adminReg'=>$adminReg,
			$res = 0;
			if(!empty($filename)){
                $res = $bulletinModel->update($condition,$filename);
			}
            if ($res) {
                $result->code = 1;
				$result->message = '操作成功';
            } else {
                $result->code = 0;
				$result->message = '操作失败';
            }

            $result->success = true;
            return $result;
        } catch (Exception $e) {
            $result->success = false;

            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
	}


	//app公告列表 只显示最新几条
	 public function appBulletinList(bulletinDTO $bulletinDTO)
    {
		$result = new ResultDO();
		$platformId = $bulletinDTO->platformId ? $bulletinDTO->platformId : 0;
		$data = $bulletinDTO->data ? $bulletinDTO->data : [];
		if(!$platformId){
			$result->message = "请填写公告内容";
			return $result;
		}

//			$num = 5;
            $items = 'id,content,updateTime,status,platformName';
            $offset = $data['offset']?$data['offset']:0;
            $num = $data['num']?$data['num']:5;
            #分页
            if ($offset == '0') {
                $offset = 1;
            }
            $limit = ($offset - 1) * $num . "," . $num;
            $groupBy = 'order by id desc limit ' . $limit;
			$condition = " platformId={$platformId} and isdelete = 1 and status = 1";
            try {
                $bulletin = new model_attendance_bulletin();
                $res = $bulletin->select($condition, $items, $groupBy)->items;
                $count = $bulletin->selectOne($condition, "count(id) num");


                if (!empty($res)) {
                    $result->data = $res;
                    $result->code = 1;
                } else {
                    $result->code = 1;
                }
                $result->counts = $count?$count['num']:0;
				$result->message = 0;
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;

	}
	
}
